package com.chen.springboot.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.springboot.entity.UserAdvice;
import com.chen.springboot.exception.ServiceException;
import com.chen.springboot.mapper.UserAdviceMapper;
import com.chen.springboot.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;

/**
 * @Author Chen
 * @Date 2024/12/19 13:39
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private UserAdviceMapper userAdviceMapper;

    @Override
    public void addAdvice(UserAdvice userAdvice) {
        // 给建议加上日期
        userAdvice.setCreateTime(new Timestamp(System.currentTimeMillis()));
        int row = userAdviceMapper.insert(userAdvice);
        if (row != 1)
            throw new ServiceException("500","建议插入数据库时发生错误");
    }

    @Override
    public Page<UserAdvice> getAdvice(Integer pageNum, Integer pageSize, String search) {
        // 根据页码和页大小获取建议
        Page<UserAdvice> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UserAdvice> wrapper = Wrappers.lambdaQuery();
        // 添加搜索词
        if (StrUtil.isNotEmpty(search))
            wrapper.like(UserAdvice::getUsername,search);
        return userAdviceMapper.selectPage(page, wrapper);
    }

    @Override
    public void deleteAdvice(Integer id) {
        int row = userAdviceMapper.deleteById(id);
        if (row != 1)
            throw new ServiceException("400","未删除成功");
    }
}
